package com.ruoyi.equip.service.impl;

import com.github.yitter.idgen.YitIdHelper;
import com.ruoyi.common.exception.custom.BizException;
import com.ruoyi.common.utils.JsonUtils;
import com.ruoyi.equip.domain.ThingModel;
import com.ruoyi.equip.domain.dto.ThingModelDto;
import com.ruoyi.equip.util.ModelCacheUtils;
import com.ruoyi.td.vo.ThingModelVo;
import com.ruoyi.equip.mapper.ThingModelMapper;
import com.ruoyi.equip.service.IThingModelService;
import com.ruoyi.td.service.IDbStructureData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

import static com.ruoyi.equip.util.ModelCacheUtils.setThingModelCache;


@Service
public class ThingModelServiceImpl implements IThingModelService {

    @Resource
    ThingModelMapper thingModelMapper;

    //@Resource
    //IDbStructureData dbStructureData;


    @PostConstruct
    public void init() {
        List<ThingModel> thingModels = thingModelMapper.selectList(null);
        thingModels.forEach(thingModel -> {
            ThingModelVo.Model model = JsonUtils.parseObject(thingModel.getModel(), ThingModelVo.Model.class);
            List<ThingModelVo.Property> properties = model.getProperties();
            setThingModelCache(thingModel.getProductKey(),properties);
        });
    }

    @Override
    public ThingModelVo findByProductKey(String productKey) {
        ThingModel thingModel = thingModelMapper.selectByKey(productKey);
        if (thingModel != null) {
            return new ThingModelVo(thingModel.getId(), thingModel.getProductKey(), JsonUtils.parseObject(thingModel.getModel(), ThingModelVo.Model.class));
        }
        return null;

    }

    @Override
    public boolean saveThingModel(ThingModelDto thingModelDto) {
        String productKey = thingModelDto.getProductKey();
        String model = thingModelDto.getModel();
        ThingModelVo oldData = findByProductKey(productKey);
        ThingModelVo thingModelVo = new ThingModelVo(YitIdHelper.nextId(), productKey, JsonUtils.parseObject(model, ThingModelVo.Model.class));

        // 验证物模型合法性
        List<ThingModelVo.Property> properties = thingModelVo.getModel().getProperties();
        for (ThingModelVo.Property property : properties) {
            //属性标识符合法性校验
            String identifier = property.getIdentifier();
            if (StringUtils.isBlank(identifier)) {
                throw new BizException("属性标识符不能为空");
            }
            if (!identifier.matches("^[a-zA-Z].*")) {
                throw new BizException("属性标识符【" + identifier + "】不合法");
            }
        }

        setThingModelCache(productKey,properties);
        boolean flag = true;
        if (oldData == null) {
            // 定义时序数据库物模型结构
            //dbStructureData.defineThingModel(thingModelVo);
        } else {
            thingModelVo.setId(oldData.getId());
            //dbStructureData.updateThingModel(thingModelVo);
            flag = false;
        }
        save(thingModelVo, flag);

        return true;
    }

    @Override
    public void delByProductKey(String productKey) {
        thingModelMapper.deleteByKey(productKey);
        ModelCacheUtils.removeThingModelCache(productKey);
    }

    public ThingModelVo save(ThingModelVo thingModelVo, boolean flag) {
        ThingModel thingModel = new ThingModel(thingModelVo.getId(), thingModelVo.getProductKey(), JsonUtils.toJsonString(thingModelVo.getModel()));
        if (flag)
            thingModelMapper.insert(thingModel);
        else
            thingModelMapper.updateById(thingModel);
        return thingModelVo;
    }

}
